package com.ruoyi.web.middleware.influxdb.impl;

import com.ruoyi.web.core.config.influx.InfluxDBProperties;
import com.ruoyi.web.middleware.influxdb.Influxdb2Service;
import com.ruoyi.web.utils.influxdb.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class Influxdb2ServiceImpl implements Influxdb2Service {

    @Autowired
    private InfluxDBProperties influxDBProperties;

//    @Autowired
//    private InfluxDBClient influxDBClient;

    @Override
    public DeviceData getLastMonitorDataMap(String deviceId , Long productId){

        if(productId == null){
            throw new RuntimeException("产品id不能为空");
        }

        FluxRecord fluxRecord = this.getLastMonitorData(deviceId, productId);

        if(fluxRecord == null){
            return null;
        }

        return new DeviceData(fluxRecord);
    }

//    //得到基础的Flux
//    private Flux getFlux(){
//        return Flux.from(influxDBProperties.getBucket()).withLocationNamed("Asia/Shanghai");
//    }
//
//    //根据设备id和产品id获取 监测数据 条件对象
//    private Restrictions getMonitorDataRestrictionsById(String deviceId , Long productId){
//        //拼接查询条件 device_properties_118
//        Restrictions restrictions = Restrictions.and(
//                Restrictions.tag(DeviceParamUtil.FIELD_DEVICE_ID).equal(deviceId),
//                Restrictions.tag(DeviceParamUtil.DB_DEFAULT_FIELD).equal(DeviceParamUtil.FIELD_NUMBER_VALUE),
//                Restrictions.measurement().equal(DeviceParamUtil.createPropertyMetric(productId)))
//                ;
//        return restrictions;
//    }

    /**
     * @desc  根据条件查询到列表
     * @author fwx
     * @date  2023/9/5 14:32
     **/
//    private List<FluxRecord> getFluxRecordList(String flux){
//
//        List<FluxRecord> fluxRecordList = new ArrayList<>();
//        List<FluxTable> fluxTableList = influxDBClient.getQueryApi().query(flux);
//
//        if(!CollectionUtils.isEmpty(fluxTableList)) {
//            fluxRecordList = fluxTableList.get(0).getRecords();
//        }
//
//        return fluxRecordList;
//    }

    @Override
    public FluxRecord getLastMonitorData(String deviceId , Long productId){
//        Flux flux = this.getFlux().range(-1L, ChronoUnit.YEARS)
//                .filter(this.getMonitorDataRestrictionsById(deviceId,productId))
//                .last()
//                .pivot(Collections.singleton("_time"), Collections.singleton("property"),"_value")
//                ;
//
//        //log.info(flux.toString());
//        List<FluxRecord> fluxRecordList = this.getFluxRecordList(flux.toString());
//
//        Optional<FluxRecord> max = fluxRecordList.stream()
//                .max(Comparator.comparing(FluxRecord::getTime));

        //TODO
        Optional<FluxRecord> max = null;

        return max.get();
    }

    @Override
    public List<DeviceData> aggregateWindow(DeviceAggreDataParam param){

//        Flux flux = this.getFlux()
//                .range(param.getStartTimeSecond(),param.getEndTimeSecond())
//                .filter(param.buildRestrictions())
//                .aggregateWindow(1L,param.getChronoUnit(),param.getType().getName());
//
//        List<FluxRecord> fluxRecordList = this.getFluxRecordList(flux.toString());
//
//        if(fluxRecordList.size() == 0){
//            return new ArrayList<>();
//        }
//
//        List<DeviceData> collect = fluxRecordList.stream()
//                .map(DeviceData::new)
//                .peek(data -> data.setDataTime(data.getDataTime().plusDays(-1)))
//                .collect(Collectors.toList());

        //TODO
        List<DeviceData> collect = null;

        return collect;
    }

    @Override
    public List<DeviceData> getFactorHistoryData(DeviceAggreDataParam param){
//        Flux flux = this.getFlux().range(param.getStartTimeSecond(),param.getEndTimeSecond())
//                .filter(param.buildRestrictions());
//
//        List<FluxRecord> fluxRecords = this.getFluxRecordList(flux.toString());
//        List<DeviceData> result = fluxRecords.stream().map(DeviceData::new).collect(Collectors.toList());

        //TODO
        List<DeviceData> result = null;

        return result;
    }

    @Override
    public List<DeviceData> getBaseHistoryData(DeviceAggreDataParam param){
//        Flux flux = this.getFlux().range(param.getStartTimeSecond(), param.getEndTimeSecond())
//                .filter(this.getBaseDataRestrictionsById(param.getDeviceId(),param.getProductId()));
//        log.info(flux.toString());
//        List<FluxRecord> fluxRecordList = getFluxRecordList(flux.toString());
//        List<DeviceData> result = fluxRecordList.stream().map(DeviceData::new).collect(Collectors.toList());

        //TODO
        List<DeviceData> result = null;

        return result;
    }

//    //根据设备id和产品id获取 基础数据 条件对象
//    private Restrictions getBaseDataRestrictionsById(String deviceId , Long productId){
//        //拼接查询条件 device_event_47_baseinfor
//        Restrictions restrictions = Restrictions.and(
//                Restrictions.tag(DeviceParamUtil.FIELD_DEVICE_ID).equal(deviceId),
//                Restrictions.measurement().equal(DeviceParamUtil.createEventBaseInfoMetric(productId)))
//                ;
//        return restrictions;
//    }

    @Override
    public DeviceData getMaxMonitorData(DeviceAggreDataParam param){

//        Flux flux = this.getFlux().range(param.getStartTimeSecond(),param.getEndTimeSecond())
//                .filter(param.buildRestrictions());
//
//         //log.info(flux.toString());
//
//        if(AggreEnum.MAX == param.getType()){
//            flux = flux.max();
//        }
//
//        if(AggreEnum.AVG == param.getType()){
//            flux = flux.mean();
//        }
//
//        if(AggreEnum.MIN == param.getType()){
//            flux = flux.min();
//        }
//
//        if(AggreEnum.SUM == param.getType()){
//            flux = flux.sum();
//        }
//
//        if(AggreEnum.FIRST == param.getType()){
//            flux = flux.first();
//        }
//
//        List<FluxRecord> fluxRecordList = this.getFluxRecordList(flux.toString());
//
//        if(fluxRecordList.size() == 0){
//            return new DeviceData();
//        }
//
//        Optional<FluxRecord> max = fluxRecordList.stream()
//                .max(Comparator.comparing(FluxRecord::getTime));
//
//        return new DeviceData(max.get());

        //TODO
        return null;
    }

    @Override
    public List<FluxRecord> getMonitorDataByTime(DeviceHistoryDataParam param) {

        //拼接查询条件
//        Restrictions restrictions = this.getMonitorDataRestrictionsById(param.getDeviceId(),param.getProductId());
//
//        Flux flux = this.getFlux()
//                .range(param.getStartTimeSecond(),param.getEndTimeSecond())
//                .filter(restrictions)
//                .pivot(Collections.singleton("_time"), Collections.singleton("property"),"_value")
//                ;
//
//        List<FluxRecord> fluxRecordList = this.getFluxRecordList(flux.toString());

        //TODO
        List<FluxRecord> fluxRecordList = null;

        return fluxRecordList;
    }

    @Override
    public List<DeviceData> getMonitorDataMapByTime(DeviceHistoryDataParam param){
        List<FluxRecord> fluxRecords = this.getMonitorDataByTime(param);
        List<DeviceData> result = fluxRecords.stream().map(DeviceData::new).collect(Collectors.toList());
        return result;
    }

}
